#Gccring - env.cpp          
#                                         
#Copyright (C) 2012 Crabant Yang
#                                         
#This file is part of pyknives.                                                       
#                                                                                   
#    Gccring is free software: you can redistribute it and/or modify                 
#    it under the terms of the GNU General Public License as published by           
#    the Free Software Foundation, either version 3 of the License, or              
#    (at your option) any later version.                                            
#                                                                                   
#    Gccring is distributed in the hope that it will be useful,                      
#    but WITHOUT ANY WARRANTY; without even the implied warranty of                 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  
#    GNU General Public License for more details.                                   
#                                                                                   
#    You should have received a copy of the GNU General Public License              
#    along with Gccring.  If not, see <http://www.gnu.org/licenses/>.

import xml.dom.minidom
import time
import os



def ip_readconfig():
	ip_xml=xml.dom.minidom.parse('iarparser.xml')
	ip_config=ip_xml.getElementsByTagName("Config")[0]
	TOOLKIT_DIR=ip_config.getElementsByTagName("TOOLKIT_DIR")[0].childNodes[0].data
	return [TOOLKIT_DIR]

def ip_read_xml(xmlname):
	xmlfile=xml.dom.minidom.parse(xmlname)
	prjNode=xmlfile.getElementsByTagName("project")[0]
	cfgs=prjNode.getElementsByTagName("configuration")
	PROJ_DIR=os.path.dirname(xmlname)
	allfiles=set()

	filetypes=['.h','.c']
	for cfg in cfgs:
		outputs=cfg.getElementsByTagName("outputs")[0]
		files=outputs.getElementsByTagName("file")
		for file in files:
			filename=file.childNodes[0].data
			parts=filename.rpartition('.')
			cur_type=parts[1]+parts[2]
			if cur_type in filetypes:
				filename=filename.replace('$PROJ_DIR$',PROJ_DIR)
				filename=filename.replace('$TOOLKIT_DIR$',TOOLKIT_DIR)
				allfiles.add(filename)
	return allfiles

def  ip_read_dir():
	files=set()
	for file in os.listdir('.'):
		if os.path.isfile(file):
			parts=file.rpartition('.')
			if parts[2] == 'dep':
				files.add(file)
	return files			


def  ip_read_workspace(filename):
	WS_DIR=os.path.dirname(filename)
	xmlfile=xml.dom.minidom.parse(filename)
	workspace=xmlfile.getElementsByTagName("workspace")[0]
	projects=workspace.getElementsByTagName("project")
	files=[]
	for project in projects:
		thepath=project.getElementsByTagName("path")[0]
		data=thepath.childNodes[0].data
		data=data.replace('$WS_DIR$',WS_DIR)
		data=data.replace('.ewp','.dep')
		files.append(data)
	return files

[TOOLKIT_DIR]=ip_readconfig()
#depfiles=ip_read_dir()
wsfile='E:\\svn\\thingweb\\product\\tnc\\software\\trunk\\WSN-OS2.0\\WTU\\TNet-WTU.eww'
depfiles=ip_read_workspace(wsfile)

fdall=os.open('all.txt',0666)
for depfile in depfiles:
	files=ip_read_xml(depfile)
	fd=os.open(depfile+'.txt',0666)
	for file in files:
		os.write(fd,file+'\n')
		os.write(fdall,file+'\n')
	os.close(fd)
os.close(fdall)

#while 1:
#	time.sleep(1)